<?php

namespace Home\Model;

use Think\Model;

class TopicModel extends Model {
	
	/**
	 * 搜索列表
	 *
	 * @param number $where        	
	 * @param number $page        	
	 * @param string $order        	
	 * @param intval $num
	 *        	每页显示条数
	 * @param intval $isPage
	 *        	是否需要分页
	 * @return unknown
	 */
	public function search($where = 1, $page = 1, $order = '', $num = '', $isPage = 1) {
		$page = $page < 1 ? 1 : $page;
		$num = $num ? $num : C ( 'page_num' );
		
		// 读取缓存
		$cacheName = get_cache_name ( $_GET ['mobile'] . 'topic_search_' . $where . $page . $order . $num . ';isPage:' . $isPage );
		$result = S ( $cacheName );
		if ($result) {
			return $result;
		}
		
		$result ['list'] = $this->where ( $where )->page ( $page, $num )->order ( $order )->select ();
		foreach ( $result ['list'] as $k => $v ) {
			$result ['list'] [$k] = $this->getInfo ( $v ['id'] );
		}
		if ($isPage == 1) {
			$result ['total_num'] = $this->where ( $where )->count ();
			$result ['page'] = page_html ( $result ['total_num'], $page, $num );
			$result ['total_page'] = ceil ( $result ['total_num'] / $num );
			if ($result ['total_page'] > $page) {
				$result ['next_page'] = $page + 1;
			} else {
				$result ['next_page'] = '';
			}
			if ($page > 1) {
				$result ['prev_page'] = $page - 1;
			} else {
				$result ['prev_page'] = '';
			}
		}
		S ( $cacheName, $result, C ( 'cache_time_short' ) );
		return $result;
	}
	/**
	 * 获取单条信息
	 *
	 * @param unknown $id        	
	 * @return Ambigous <\Think\mixed, boolean, NULL, multitype:, mixed, unknown, string, object>
	 */
	public function getInfo($id) {
		
		// 读取缓存
		$cacheName = get_cache_name ( 'topic_' . $id );
		$result = S ( $cacheName );
		if ($result) {
			return $result;
		}
		
		if (intval ( $id )) {
			$where = 'id=' . intval ( $id );
		} else {
			$where = 'url="' . $id . '"';
		}
		
		$result = $this->where ( $where )->find ();
		
		$result ['url'] = C ( 'site_url' ) . '/topic/' . $result ['url'] . '/';
		S ( $cacheName, $result, C ( 'cache_time_long' ) );
		
		return $result;
	}
	
	
}
